<template>
  <div id="home">
    <!-- 顶部导航栏区域 -->
    <navbar class="navbar">
      <div class="center" slot="center">购物车</div>
    </navbar>
    <!-- <tabcontrol
      :titles="['流行', '新款', '精选']"
      @tabclick="clickitem"
      class="tab-control"
    ></tabcontrol> -->
    <scroll
      class="allcontent"
      @scroll="scrollcontent"
      ref="scroll"
      :probeType="3"
      :pullUpload="true"
      @pullupload="uploadmore"
    >
      <!-- 轮播图 -->
      <swiper :banners="banners" class="swiper" ref="swiper"></swiper>
      <!-- 特征区域 -->
      <a href="">
        <img src="~assets/img/home/homeFeature.png" alt="" />
      </a>
      <tabcontrol
        :titles="['流行', '新款', '精选']"
        @tabclick="clickitem"
        ref="tabcontrol"
        :class="{ tabControl: showtabcontrol }"
      ></tabcontrol>
      <goods-list :goodList="GoodsList"></goods-list>
    </scroll>
    <backtop v-show="isShow" @click.native="backClick" />
  </div>
</template>

<script>
import navbar from "components/common/navbar/NavBar.vue";
import swiper from "../../components/swiper/swiper.vue";
import tabcontrol from "./childcops/tabControl.vue";
import GoodsList from "../home/childcops/GoodsList.vue";
import { getdata, gethomegoods } from "network/home.js";
import scroll from "../../components/common/scroll/Scroll.vue";
import Backtop from "../../components/common/backtop/Backtop.vue";
export default {
  data() {
    return {
      currenttype: "pop",
      banners: [],
      GoodsList: [],
      goods: {
        new: {
          page: 0,
          list: [],
        },
        pop: {
          page: 0,
          list: [],
        },
        sell: {
          page: 0,
          list: [],
        },
      },
      isShow: false,
      saveY: 0,
      tabcontroloffsettop: 0,
      showtabcontrol: false,
    };
  },
  components: {
    navbar,
    swiper,
    tabcontrol,
    GoodsList,
    scroll,
    Backtop,
  },
  created() {
    this.getLunbo();
    // 获取三个类别的数据
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
    // 默认选中pop
    this.clickitem("pop");
    // 监听图片加载完成
    this.$bus.$on("loadimage", () => {
      this.$refs.scroll.refresh();
    });
  },
  methods: {
    // 获得轮播图内容
    getLunbo() {
      getdata().then((res) => {
        this.banners = res.data.data.banner.list;
        console.log(this.banners);
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      gethomegoods(type, page).then((res) => {
        console.log(res);
        this.goods[type].list.push(...res.data.data.list);
        this.goods[type].page += 1;
        this.$refs.scroll.finishPullup();
      });
    },
    clickitem(type) {
      this.GoodsList = this.goods[type].list;
      this.currenttype = type;
    },
    // 监听屏幕滚动
    scrollcontent(position) {
      this.isShow = -position.y > 100;
      this.showtabcontrol = -position.y > this.tabcontroloffsettop;
      console.log(this.tabcontroloffsettop);
      console.log(-position.y);
    },
    backClick() {
      this.$refs.scroll.scrollTo(0, 0);
    },
    // 上拉加载更多
    uploadmore() {
      this.getHomeGoods(this.currenttype);
      console.log(this.currenttype);
    },
  },
  destroyed() {
    console.log("销毁");
  },
  activated() {
    // console.log("acti");
    this.$refs.scroll.scrollTo(0, this.saveY, 0);
  },
  deactivated() {
    this.saveY = this.$refs.scroll.scroll.y;
    // console.log(this.saveY);
  },
  mounted() {
    this.tabcontroloffsettop = this.$refs.tabcontrol.$el.offsetTop;
    console.log(this.$refs.tabcontrol.$el.offsetTop);
    // console.log(this.$refs.swiper.$el);
  },
};
</script>

<style scoped>
.center {
  line-height: 44px;
  color: white;
}
.navbar {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9;
  right: 0;
}
.swiper {
  margin-top: 44px;
}
.tabControl {
  position: relative;
  /* z-index: 10; */
  background-color: #fff;
  top: 44px;
  left: 0;
  margin-top: 44px;
  /* z-index: 1222; */
  right: 0;
}
.allcontent {
  height: 400px;
  /* background-color:#000;
  z-index: 1000; */
  /* overflow: hidden; */
}
</style>
